<!--
 * @Description: 企业消息配置录入
 * @Version: 1.0
 * @Author: Yuanjy
 * @Date: 2020-03-13 19:56:08
 * @LastEditors: Yuanjy
 * @LastEditTime: 2020-05-13 10:29:07
 -->
<template>
  <el-dialog :append-to-body="true" :visible.sync="dialog" :title="isAdd ? '新增企业信息' : '编辑企业信息'" width="700px">
    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="130px">
      <el-form-item label="消息类型" prop="type">
        <el-select v-model="form.type" size="small" placeholder="订单类型选择" style="width:85%">
          <el-option v-for="(item, index) in types" :key="index" :label="item" :value="index" />
        </el-select>
      </el-form-item>
      <el-form-item label="消息子类型" prop="sub_type">
        <el-select v-model="form.sub_type" size="small" placeholder="订单类型选择" style="width:85%">
          <el-option v-for="(item, index) in subTypes[form.type]" :key="index" :label="item" :value="index" />
        </el-select>
      </el-form-item>
      <el-form-item label="消息句柄" prop="send_handle">
        <el-input v-model="form.send_handle" style="width:85%" />
      </el-form-item>

      <el-form-item label="配置类型">
        <el-switch v-model="jsonEditor" :active-value="true" :inactive-value="false" active-color="#13ce66" inactive-color="#ff4949" active-text="JSON" inactive-text="文案" />
      </el-form-item>
      
      <el-form-item label="消息配置">
        <template v-if="jsonEditor">
          <json-editor ref="jsonEditor" v-model="form.config" style="line-height: 16px !important;" />
        </template>
        <template v-else>
          <el-input :rows="3" v-model="form.config" type="textarea" placeholder="请填写配置值" style="width:85%" />
        </template>
      </el-form-item>
      <!-- <el-form-item label="是否启用">
        <el-switch v-model="form.enabled" active-color="#13ce66" inactive-color="#ff4949" active-value="1" inactive-value="0" active-text="启用" inactive-text="停用" />
      </el-form-item> -->
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import jsonEditor from '@/views/components/jsonEditor'

export default {
  components: {
    jsonEditor
  },
  props: {
    isAdd: {
      type: Boolean,
      required: true
    },
    supThis: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      loading: false,
      dialog: false,
      jsonEditor: false,
      form: {
        id: '',
        type: '',
        sub_type: '',
        send_handle: '',
        config: '',
        enabled: '1'
      },
      types: [],
      subTypes: [],
      rules: {
        type: [
          { required: true, message: '请输入消息类型', trigger: 'blur' },
        ],
        sub_type: [
          { required: true, message: '请输入消息子类型', trigger: 'blur' },
        ],
        send_handle: [
          { required: true, message: '请输入发送消息句柄', trigger: 'blur' },
        ],
      }
    }
  },
  methods: {
    initParams() {
      this.$api.enterprise.messageConfig
        .getParams({
          params: { keys: ['types', 'sub_types'] }
        }).then(res => {
          this.types = res.data.types
          this.subTypes = res.data.sub_types
        }).catch(err => {
          console.log(err)
        })
    },
    cancel() {
      this.resetForm()
    },
    doSubmit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (typeof this.form.config === 'object') {
            this.form.config = JSON.stringify(this.form.config)
          }
          this.loading = true
          if (this.isAdd) {
            this.doAdd()
          } else this.doEdit()
        } else {
          return false
        }
      })
    },
    doAdd() {
      this.$api.enterprise.messageConfig
        .add({
          data: this.form
        }).then(res => {
          this.resetForm()
          this.$notify({ title: '添加成功', type: 'success', duration: 2500 })
          this.loading = false
          this.$parent.$parent.init()
        }).catch(err => {
          this.loading = false
          console.log(err)
        })
    },
    doEdit() {
      this.$api.enterprise.messageConfig
        .edit({
          data: this.form
        }).then(res => {
          this.resetForm()
          this.$notify({ title: '修改成功', type: 'success', duration: 2500 })
          this.loading = false
          this.supThis.init()
        }).catch(err => {
          this.loading = false
          console.log(err)
        })
    },
    resetForm() {
      this.dialog = false
      this.$refs['form'].resetFields()
      this.form = {
        id: '',
        type: '',
        sub_type: '',
        send_handle: '',
        config: '',
        enabled: '1'
      }
    }
  }
}
</script>

<style scoped>

</style>
